package com.aopeng.controller;

import com.aopeng.entity.AopengExamination;
import com.aopeng.entity.AopengNavcolumn;
import com.aopeng.entity.AopengSchool;
import com.aopeng.mapper.AopengExaminationMapper;
import com.aopeng.mapper.AopengNavcolumnMapper;
import com.aopeng.mapper.AopengSchoolMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * zqh
 * 对栏目的增删改查
 */
@Controller
public class AopengNavcolumnController {
    @Autowired
    private AopengNavcolumnMapper aopengNavcolumnMapper;
    @Autowired
    private AopengSchoolMapper aopengSchoolMapper;
    @Autowired
    private AopengExaminationMapper aopengExaminationMapper;
    @Autowired
    private AopengExaminationController aopengExaminationController;

    //获取所有的数据
    @RequestMapping("/getAopengNavcolumn")
    public  String getAopengNavcolumn(Model model){
        System.out.println("查询全部");
        List<AopengNavcolumn> aopengNavcolumns = aopengNavcolumnMapper.selectList(null);
        model.addAttribute("getAopengNavcolumn",aopengNavcolumns);
        return "admin/AopengNavcolumn";
    }

    //根据id获取数据
    @RequestMapping("/getAopengNavcolumnById")
    public String getAopengNavcolumnById(AopengNavcolumn aopengNavcolumn,Model model){
        System.out.println("根据id查询");
        //获取前台传过来的数据
        AopengNavcolumn getAopengNavcolumnById = aopengNavcolumnMapper.selectById(aopengNavcolumn.getId());
        model.addAttribute("getAopengNavcolumnById",getAopengNavcolumnById);
        return "admin/updata-AopengNavcolumn";
    }

    //根据id修改数据
    @RequestMapping("/updateAopengNavcolumnById")
    public String updateAopengNavcolumnById(AopengNavcolumn aopengNavcolumn){
        System.out.println("根据id修改数据");
        System.out.println(aopengNavcolumn.getId());
        //获取前台传过来的数据修改
        aopengNavcolumnMapper.updateById(aopengNavcolumn);

        AopengSchool aopengSchool = new AopengSchool();
        aopengSchool.setId(aopengNavcolumn.getId());
        aopengSchool.setSchoolName(aopengNavcolumn.getColumnName());
        aopengSchoolMapper.updateById(aopengSchool);
        return "redirect:/getAopengNavcolumn";
    }

    //添加数据
    @RequestMapping("/insertAopengNavcolumn")
    public String insertAopengExamination(AopengNavcolumn aopengNavcolumn,int xiala){
        AopengSchool aopengSchool = new AopengSchool();
        aopengSchool.setSchoolType(xiala);
        aopengSchool.setSchoolName(aopengNavcolumn.getColumnName());
        aopengSchoolMapper.insert(aopengSchool);

        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("school_name",aopengNavcolumn.getColumnName());//写表中的列名
        List<AopengSchool> map1 = aopengSchoolMapper.selectByMap(columnMap);
        AopengSchool aopengSchool1 = map1.get(0);
        aopengNavcolumn.setExaminationId(aopengSchool1.getId());
        aopengNavcolumnMapper.insert(aopengNavcolumn);

        return "redirect:/getAopengNavcolumn";
    }

    //跳转
    @RequestMapping("/insertAopengExaminationtiaozhuang")
    public String insertAopengExaminationtiaozhuang(){
        return "admin/insert-AopengNavcolumn";
    }

    //根据id删除
    @RequestMapping("/deleteAopengNavcolumnById")
    public String deleteAopengNavcolumnById(AopengNavcolumn aopengNavcolumn){
        System.out.println("根据id删除数据"+aopengNavcolumn.getExaminationId());
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("school_id",aopengNavcolumn.getExaminationId());
        /*aopengExaminationMapper.deleteByMap(columnMap);*/
        List<AopengExamination> getAopengExamination = aopengExaminationMapper.selectByMap(columnMap);
        for (AopengExamination str: getAopengExamination) {
            aopengExaminationController.deleteAopengExaminationById(str.getId());
        }
        //AopengNavcolumn aopengNavcolumn1 = aopengNavcolumnMapper.selectById(aopengNavcolumn.getExaminationId());
        aopengSchoolMapper.deleteById(aopengNavcolumn.getExaminationId());
        aopengNavcolumnMapper.deleteById(aopengNavcolumn.getId());
        return "redirect:/getAopengNavcolumn";

    }
}
